﻿Option Explicit On
Option Strict On

Imports LogginsLogDSTableAdapters

Partial Class private_default
    Inherits System.Web.UI.Page
    Dim u As MembershipUser
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        ''Try

        ''If Not Request.QueryString("id") Is Nothing Then
        ''    u = Membership.GetUser(Request.QueryString("id"))
        ''    If Not u.IsApproved Then
        ''        u.IsApproved = True
        ''        Membership.UpdateUser(u)
        ''    End If
        ''End If
        'Dim emailverif As Boolean = False
        'If Not Request.QueryString("id") Is Nothing Then
        '    Dim MyGuid As Guid = New Guid(Request.QueryString("id"))
        '    u = Membership.GetUser(MyGuid)
        '    lblUserName.Text = u.UserName
        '    emailverif = True
        '    Session("UserName1") = ""
        '    FormsAuthentication.SignOut()
        'Else
        '    If Not Session("UserName1") Is Nothing AndAlso Not Session("UserName1").ToString() = "" Then
        '        Dim username As String = CStr(Session("UserName1"))
        '        u = Membership.GetUser(username)
        '        lblUserName.Text = username
        '    ElseIf User.Identity.IsAuthenticated Then

        '        lblUserName.Text = User.Identity.Name.ToString()
        '        u = Membership.GetUser(User.Identity.Name.ToString())
        '    Else
        '        Response.Redirect("~/Account/Login.aspx")
        '    End If
        'End If

        'If emailverif Then
        '    u.IsApproved = True
        '    Membership.UpdateUser(u)
        '    'HyperLink1.Visible = True
        'End If
        ''If User.Identity.IsAuthenticated Then
        ''    u = Membership.GetUser(User.Identity.Name)
        ''    u.IsApproved = False
        ''    FormsAuthentication.SignOut()  ' what does this do??
        ''End If
        'u = Membership.GetUser(lblUserName.Text)

        Dim getuseracccheckdataTA As New usp_UserCheckTableAdapter()
        Dim getuseracccheckdataDT As LogginsLogDS.usp_UserCheckDataTable = Nothing

        u = Membership.GetUser(User.Identity.Name.ToString())
        If Not u Is Nothing Then
            Try
                'Dim o As Object = u.ProviderUserKey
                getuseracccheckdataDT = getuseracccheckdataTA.GetData(CType(u.ProviderUserKey, Global.System.Guid))
            Catch ex As Exception
                Throw ex
            End Try
        Else
            'send user to the login page
            FormsAuthentication.SignOut()
            Response.Redirect("~/account/login.aspx")
        End If

        ' Valid users enter this for loop, invalid users do not!
        For Each getuseracccheckdataRow As LogginsLogDS.usp_UserCheckRow In getuseracccheckdataDT

            'Check #1
            If Not getuseracccheckdataRow.AccountActive Then
                'if the account is not active do not all the user to remain logged in

                lblInfo.Text += "AccountActive is false</br> "
                Response.Redirect("~/AccountNotActive.aspx")
            Else
                lblInfo.Text += "AccountActive is true</br> "
            End If

            'Check #2
            If Not getuseracccheckdataRow.AccountInCredit = True Then
                'send the user to their payment page to purchase credits -- future phase
                'then back to this page
                AccountNotInCreditModalPopupExtender.Show()
                'Response.Redirect("~/AccountNotInCredit.aspx")
                'lblInfo.Text += "AccountInCredit is false go to future phase</br>"
                'Ned:This part comes later and there I'll change Account in credit
            Else
                'lblInfo.Text += "AccountInCredit is true</br>"
            End If


            'This check needs to come before the Approval Check
            'Check #3
            If Not getuseracccheckdataRow.RegistrationComplete Then
                'send to user to a page where they can complete their registration
                'Collect the person data and person entitydata 
                'use the procedure dbo.usp_AddNewPerson
                'then back to this page
                If Not User.Identity.IsAuthenticated Then
                    Session("UserName1") = u.UserName
                    Response.Redirect("~/private/RegComplete.aspx")
                Else
                    Response.Redirect("~/private/RegComplete.aspx")
                End If

                'lblInfo.Text += "RegistrationComplete is false</br> "

            Else
                lblInfo.Text += "RegistrationComplete is true</br> "
            End If

            'Check #4
            If getuseracccheckdataRow.ApprovalRequired = False Then
                If u.IsApproved = False Then
                    'approve the account
                    u.IsApproved = True
                    'Roles.RemoveUserFromRole(u.UserName, "AwaitingApproval")
                    'Roles.AddUserToRole(u.UserName, "ListManager")

                    lblInfo.Text += "Account  is Activated right now</br>"
                    'HyperLink1.Visible = True
                Else
                    If getuseracccheckdataRow.AwaitingApproval Then
                        'if the user registered over a day ago
                        'send reminder email to administrator

                        'NENAD: Please only send the email below if the time elapsed between the signup and now is 24 hours

                        'Dim strMailFrom As String = ConfigurationManager.AppSettings("smtpmailfrom").ToString()
                        'Dim strMailTo As String = ConfigurationManager.AppSettings("accountapprovalemail").ToString()
                        'Dim virtualurl As String = "http://" + HttpContext.Current.Request.Url.Authority.ToString() + "/" + _
                        '    HttpContext.Current.Request.Url.Segments(1).ToString() + "Account/Activate.aspx?id="
                        ''
                        'Dim sendemail As New SendEmailViaGoogle()
                        'sendemail.SendEmail(strMailFrom, strMailTo, "Account Approval Request Pending", _
                        '                   "A user is still waiting for you to approve his account. " + virtualurl + u.ProviderUserKey.ToString())

                        lblInfo.Text += "AwaitingApproval is true email sent to the admin</br>"

                        'Have to send the user off this page otherwise it will loop.
                        Response.Redirect("~/ApprovalReminder.aspx")
                    Else
                        lblInfo.Text += "AwaitingApproval is false</br>"
                    End If
                End If
                'HyperLink2.Visible = True
            End If

            'Check #5
            'Ned: User can't log in without email confimed... This check is obsolete!!! 
            'If Not getuseracccheckdataRow.EmailConfirmed AndAlso emailverif = False Then
            '    'The user has logged attempted to log in without confirming their password
            '    'Remind the user to confirm their email using the email sent previously
            '    'then Log the user off 
            '    lblInfo.Text += "EmailConfirmed is false</br> "
            '    Response.Redirect("~/AccountEmailNotConfirmed.aspx")
            'Else
            '    lblInfo.Text += "EmailConfirmed is true</br> "
            'End If

            'Check #6
            'Check that the user has filled in their role but dont check if an Associate
            If Not Roles.IsUserInRole("ListAssociate") Then  '<<<<<<<<<<<<<<<<
                If Not getuseracccheckdataRow.UserRoleSet Then
                    Response.Redirect("~/private/RegRole.aspx")
                Else
                    lblInfo.Text += "UserRoleSet is true</br> "
                End If
            Else
                lblInfo.Text += "UserRoleSet is true</br> "
            End If

            'Check #7
            'check that the user has filled in their entity data
            'If the user is in the ListAssociate Role then they skip this
            'but they should have had their AssociateEntitySet already
            If Not Roles.IsUserInRole("ListAssociate") Then
                If Not getuseracccheckdataRow.AssociatedEntitySet Then
                    Response.Redirect("~/private/RegEntity.aspx")
                Else
                    lblInfo.Text += "AssociatedEntity is true</br> "
                End If
            Else
                lblInfo.Text += "AssociatedEntity is true</br> "
            End If

            'Check #8
            'if this person is an associate and their sponsor's account is not active don't allow access
            If Roles.IsUserInRole("ListAssociate") Then

                Dim NewPersonAssociateRequestemailTA As New LogginsLogDSTableAdapters.usp_PersonAssociateRequestTableAdapter()
                Dim NewPersonAssociateRequestEmailDT As LogginsLogDS.usp_PersonAssociateRequestDataTable
                Try
                    NewPersonAssociateRequestEmailDT = NewPersonAssociateRequestemailTA.GetData(CType(u.ProviderUserKey, Global.System.Guid))
                Catch ex As Exception
                    Throw ex
                End Try

                'If NewPersonAssociateRequestEmailDT.Count = 0 Then
                '    Dim personid As Integer = newpersonassociaterequestemailRow.PersonId  'Get the personid that created this Associate 

                '    Dim SPTA As New LogginsLogDSTableAdapters.SPTableAdapter()
                '    Dim bPersonAccountIsActive As Boolean = CBool(SPTA.fn_UserAccountIsActive(personid)) 'Check if the person's account is still active

                '    If Not bPersonAccountIsActive Then
                '        '    'Add another check here to see if the account of the person who invited the sponsor is active
                '        '    'AssociateValidate.aspx()
                '        '    'use this function to check prevent an individual from logging in if their sponsor's account is not active - select dbo.fn_UserAccountIsActive(65) 
                '        lblInvitationMessage.Text = "Sorry, Your Sponsors's Account is not/no longer Active."
                '        InvitationFailModalPopupExtender.Show()

                '    Else

                '    End If
                'End If
            End If

            ' All done, let the user into the secure area
            Response.Redirect("~/private/secure_members/default.aspx")
        Next

        'Something is wrong is the user has reached here. 
        'The user should have gone through the conduit above and then been stopped by one of the steps above
        'The user should have entered the loop above and then into the secure area
        'It's likely that the user has a user name but not yet registered to the system

        'Send the user back to the outer home page
        'The real fix is to delete the user then let them register themselves again
        Response.Redirect("~/default.aspx")

    End Sub

End Class

